import { defineStore } from "pinia";
import { store } from "@/store";

export const useKeepAliveViewStore = defineStore({
  id: "keepAlive-view",
  state: () => ({
    // 缓存页面 keepAlive
    keepAliveViewList: []
  }),
  actions: {
    addKeepAliveView(view) {
      // 不重复添加
      if (this.keepAliveViewList.includes(view.name)) return;
      if (view?.meta?.keepAlive) {
        this.keepAliveViewList.push(view.name);
      }
    },
    delKeepAliveView(view) {
      const index = this.keepAliveViewList.indexOf(view.name);
      index > -1 && this.keepAliveViewList.splice(index, 1);
    },
    delAllKeepAliveViews() {
      this.keepAliveViewList = [];
    }
  }
});

export function useKeepAliveViewStoreHook() {
  return useKeepAliveViewStore(store);
}
